package com.qiangesoft.cloud.feignconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qiangesoft.cloud.feignconsumer.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/order")
@RestController
//@DefaultProperties(defaultFallback = "addFail1") // 类级别默认熔断配置
public class OrderController {

    @Autowired
    private PaymentService paymentService;

    // 方法级别熔断配置
//    @HystrixCommand(fallbackMethod = "addFail", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
//    @HystrixCommand
    @GetMapping("/add")
    public Map<String, Object> add(@RequestParam("name") String name) {
        Map<String, Object> map = new HashMap<>(4);

//        int a = 5 / 0;

        String s = paymentService.add(name);
        map.put("payId", s);
        map.put("orderId", UUID.randomUUID().toString().replace("-", ""));

        System.out.println(name + "下单成功");
        return map;
    }

    public Map<String, Object> addFail(String name) {
        Map<String, Object> map = new HashMap<>(4);

        map.put("payId", "系统繁忙，请稍后再试！");
        map.put("orderId", UUID.randomUUID().toString().replace("-", ""));

        return map;
    }

    public Map<String, Object> addFail1() {
        Map<String, Object> map = new HashMap<>(4);

        map.put("payId", "系统繁忙，请稍后再试！");
        map.put("orderId", UUID.randomUUID().toString().replace("-", ""));

        return map;
    }

    @GetMapping("/addHystrix")
    public Map<String, Object> addHystrix(@RequestParam("name") String name) {
        Map<String, Object> map = new HashMap<>(4);

        String s = paymentService.addHystrix(name);
        map.put("payId", s);
        map.put("orderId", UUID.randomUUID().toString().replace("-", ""));

        System.out.println(name + "下单成功");
        return map;
    }
}
